<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="dAdd"
                >新增角色</el-button
              >
            </el-row>
            <!-- 表格 -->
            <el-table :data="roles">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="hEdit(scope.row)"
                    >编辑</el-button
                  >
                  <el-button
                    size="small"
                    type="danger"
                    @click="dDel(scope.row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>

            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->

              <el-pagination
                background
                layout="prev,pager,next"
                :page-size="pageParams.pagesize"
                :total="total"
                @current-change="hCurrentChange"
                :current-page.sync="pageParams.page"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 新增弹框 -->
    <el-dialog
      :title="isEdit ? '编辑' : '新增'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialog"
      @close="hClose"
    >
      <el-form label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="showDialog=false">取消</el-button>
          <el-button size="small" type="primary" @click="hSubmit"
            >确定</el-button
          >
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getRoles, deleteRole, addRole, updateRole } from "@/api/setting";
export default {
  data() {
    return {
      isEdit: false,
      roleForm: {
        name: "",
        description: "",
      },
      showDialog: false,
      // 后台返回的数据
      roles: [],
      pageParams: {
        page: 1, // 当前页码
        pagesize: 2, // 每页条数
      },
      total: 0, // 总的数据条数
    };
  },
  created() {
    this.loadRoles();
  },
  methods: {
    // 获取数据
    async loadRoles() {
      const res = await getRoles(this.pageParams);
      console.log("编辑", res);
      this.roles = res.data.rows;
      this.total = res.data.total;
    },
    // 页码
    hCurrentChange(curPage) {
      this.pageParams.page = curPage;
      this.loadRoles();
    },
    // 删除
    async doDel(id) {
      try {
        const res = await deleteRole(id);
        console.log(res);
        // 1. 提示
        this.$message.success("删除成功");
        // 2. 如果当前表格只有最后一条,删除后自动请求上一页数据
        if (this.roles.length === 1) {
          this.pageParams.page--;
          if (this.pageParams <= 0) {
            this.pageParams.page === 1;
          }
        }
        // 3. 重新请求
        this.loadRoles();
      } catch (err) {
        console.log(err);
        this.$message.error("删除失败");
      }
    },
    // 删除
    dDel(id) {
      this.$confirm("此操作将删除部门, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          // 调用删除api
          this.doDel(id);
        })
        .catch(() => {
          // 用户取消退出
        });
    },
    dAdd() {
      this.showDialog = true;
    },
    async doAdd() {
      try {
        const res = await addRole(this.roleForm);
        console.log(res);
        this.$message.success("添加成功");
        this.showDialog = false;
        this.total++;
        this.pageParams.page = Math.ceil(this.total / this.pageParams.pagesize);
        this.loadRoles();
      } catch (err) {
        this.$message.error("添加失败");
        console.log(err);
      }
    },
    // 添加
    hSubmit() {
      if (this.isEdit) {
        this.doEdit();
      } else {
        this.doAdd();
      }
    },
    hEdit(row) {
      this.isEdit = true;
      this.showDialog = true;
      this.roleForm = {...row};
    },
    async doEdit() {
      await updateRole(this.roleForm);
      this.$message.success("编辑成功");
      this.showDialog = false;
      this.loadRoles();
    },
    hClose(){
      this.roleForm={
        name: "",
        description: "",
      }
    }
  },
};
</script>
